package com.kye.express.business.jd.address_select;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemAddressSelectioinBinding;
import com.kye.model.bean.address.AddressBean;

import java.util.List;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 01/02/2018
 * @updated : 01/02/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class AddressSelectViewBinder extends ItemViewBinder<AddressBean, AddressSelectViewBinder.ViewHolder> {

    private OnAddressListener addressListener;

    public AddressSelectViewBinder(OnAddressListener addressListener) {
        this.addressListener = addressListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_address_selectioin, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull AddressBean item) {
        holder.bindData(item);
        char firstLetter = item.getFirstLetter();
        int sectionForPosition = getSectionForPosition(firstLetter);
        int positionForSection = getPositionForSection(sectionForPosition);
        int adapterPosition = holder.getAdapterPosition();
        if (adapterPosition == positionForSection) {
            holder.binding.letter.setVisibility(View.VISIBLE);
            holder.binding.letter.setText(String.valueOf(item.getFirstLetter()));
        } else {
            holder.binding.letter.setVisibility(View.GONE);
        }
    }

    public int getSectionForPosition(char letter) {
        return letter;
    }

    public int getPositionForSection(int section) {
        List<AddressBean> datas = (List<AddressBean>) getAdapter().getItems();
        /**
         * 每一个item都必须要遍历一次, 存在性能问题
         */
        for (int i = 0; i < datas.size(); i++) {
            char fristLetter = datas.get(i).getFirstLetter();
            if (fristLetter == section) {
                return i; // 相同的一组会返回第一个item的位置
            }
        }
        return -1;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public AddressBean item;
        public ItemAddressSelectioinBinding binding;

        public ViewHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
            RxView.clicks(binding.address)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            if (addressListener != null) {
                                addressListener.onAddressClicked(item);
                            }
                        }
                    });
        }

        public void bindData(AddressBean item) {
            this.item = item;
            binding.setData(item);
        }
    }

    public interface OnAddressListener {
        void onAddressClicked(AddressBean item);
    }

}
